package com.joke.mydiary;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.io.IOException;

public class AlarmShowActivity extends Activity {

    private MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window win=getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        setContentView(R.layout.content_alarm_show);

       SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
        String ring=sp.getString("ring_preference", "ls1");

        if("nokia".equals(ring))
        {
            mediaPlayer=MediaPlayer.create(this, R.raw.nokia);
        }else if("moto".equals(ring))
        {
            mediaPlayer=MediaPlayer.create(this, R.raw.moto);
        }else {
            mediaPlayer=MediaPlayer.create(this, R.raw.ls1);
        }
        mediaPlayer.setLooping(true);
            mediaPlayer.start();
        showDialog();
    }

    private void showDialog()
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("备忘录提醒");
        builder.setIcon(android.R.drawable.ic_dialog_info);
        Intent intent=getIntent();
        builder.setMessage(" "+intent.getStringExtra("content"));
        builder.setPositiveButton("取消提醒", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mediaPlayer.stop();
                finish();
            }
        });
        builder.setNegativeButton("五分钟后提醒", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mediaPlayer.stop();
                AlarmManager alarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent intent=getIntent();
                PendingIntent pi=PendingIntent.getActivity(AlarmShowActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                long triggerTime=System.currentTimeMillis()+300000;
                alarmManager.set(AlarmManager.RTC_WAKEUP,triggerTime,pi);
                Toast.makeText(AlarmShowActivity.this, "提醒设置成功！", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
                finish();
            }
        });
        builder.show();
    }

    //销毁时停止播放音乐
    @Override
    protected void onDestroy() {
        mediaPlayer.release();
        super.onDestroy();
    }


}
